Page({

    data:{
        userid:'s',
        ename:'',
        phone:'',
        palce:'',
        desc: '',
        date: '2016-09-01',
        hospitalarrays:[],
        hospitalpk:[],
        hospitalindex:0,
        danweiarrays:[],
        danweipk:[],
        danweiindex:0,
        fenleiarrays:[],
        fenleipk:[],
        fenleiindex:0,
        imagefile:'https://www.baidu.com/img/bd_logo1.png',
        imagefiles:[],
    },

    /**
     * 监听日期picker
     * @param e
     */
    bindDateChange:function (e) {
        this.setData({
            date: e.detail.value
        })
    },

    /**
     * 监听故障院区picker
     * @param e
     */
    bindHospitalChange:function (e) {
        var that=this
        wx.request({
            url: 'http://app.benqmedicalcenter.com:8090/QueryCode.ashx?',
            data: {
                state:'danwei',
                value:this.data.hospitalarrays[this.data.hospitalindex].hospital
            },
            method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
            // header: {}, // 设置请求的 header
            success: function(res){
                // success
                that.setData({
                   danweiarrays:res.data
                })
                var arr=[]
                for(var i=0;i<res.data.length;i++)
                {
                    arr[i]=res.data[i].CODE_DESC
                }
                that.setData({
                   danweipk:arr
                })
                console.log(that.data.danweipk)
            },
            fail: function() {
                // fail
            },
            complete: function() {
                // complete
            }
        })
    },

    /**
     * 监听维修单位picker
     * @param e
     */
    bindDanweiChange:function (e) {
        var that=this
        wx.request({
            url: 'http://app.benqmedicalcenter.com:8090/QueryCode.ashx?',
            data: {
                state:'fenlei',
                value:this.data.danweiarrays[this.data.danweiindex].CODE_VALUE
            },
            method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
            // header: {}, // 设置请求的 header
            success: function(res){
                // success
                that.setData({
                    fenleiarrays:res.data
                })
                var arr=[]
                for(var i=0;i<res.data.length;i++)
                {
                    arr[i]=res.data[i].CODE_DESC
                }
                that.setData({
                    fenleipk:arr
                })
                console.log(that.data.fenleipk)
                console.log(that.data.array)
            },
            fail: function() {
                // fail
            },
            complete: function() {
                // complete
            }
        })
    },


    /**
     * 监听维修分类picker
     * @param e
     */
    bindFenleiChange:function (e) {
    },

    /**
     * 监听故障地点input
     * @param e
     */
    listenerPlaceInput:function (e) {
           this.setData(
               {
                 palce:e.detail.value
               }
           )
    },

    /**
     * 监听故障描述input
     * @param e
     */
    listenerDescInput:function (e) {
        this.setData({
            desc:e.detail.value
        })
    },

    /**
     * 监听图片
     * @param e
     */
    listenerImage:function (e) {
          var that=this
           wx.chooseImage({
             count: 5, // 最多可以选择的图片张数，默认9
             sizeType: ['original', 'compressed'], // original 原图，compressed 压缩图，默认二者都有
             sourceType: ['album', 'camera'], // album 从相册选图，camera 使用相机，默认二者都有
             success: function(res){
                 var arra=[]
                 arra[0]=res.tempFilePaths
                 that.setData(
                     {
                         imagefile:res.tempFilePaths,
                         imagefiles:arra
                     }
                 )
               // success
             },
             fail: function() {
               // fail
             },
             complete: function() {
               // complete
             }
           })
    },

    /**
     * 监听添加按钮
     * @param e
     */
    listenerAdd:function (e) {
        var errdesc="故障地点："+ this.data.palce+"\n故障描述："+this.data.desc;
        wx.request({
          url: 'http://app.benqmedicalcenter.com:8090/AddWarranty.ashx',
          data: {
              expect_date:this.data.date,
              err_user:this.data.ename,
              err_phone:this.data.phone,
              err_desc:errdesc,
              hospital:'南京明基',
              equ_sort:this.data.fenleiarrays[this.data.fenleiindex].CODE_VALUE,
              equ_sort_desc:this.data.fenleiarrays[this.data.fenleiindex].CODE_DESC,
          },
          method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
          // header: {}, // 设置请求的 header
          success: function(res){
            // success
              console.log(res.data)
              if (res.data=="success") {
                  wx.redirectTo({
                      url: '../history/test',
                      success: function (res) {
                          // success
                      },
                      fail: function () {
                          // fail
                      },
                      complete: function () {
                          // complete
                      }
                  })
              }
          },
          fail: function() {
            // fail
          },
          complete: function() {
            // complete
          }
        })
        console.log("as")
        console.log(this.data.imagefiles[0])
        var tempFilePaths=this.data.imagefile
     /* wx.uploadFile({
          url: 'http://localhost:26762/AddWarranty.ashx',
          filePath:tempFilePaths[0],
          name:'1234.jpg',
          // header: {}, // 设置请求的 header
           formData: {
               expect_date:this.data.date,
               err_user:this.data.ename,
               err_phone:this.data.phone,
               err_desc:errdesc,
               hospital:'南京明基',
               equ_sort:this.data.fenleiarrays[this.data.fenleiindex].CODE_VALUE,
               equ_sort_desc:this.data.fenleiarrays[this.data.fenleiindex].CODE_DESC,
           }, // HTTP 请求中其他额外的 form data
          success: function(res){
            // success
              console.log("成功")
          },
          fail: function() {
            // fail
              console.log("失败")
          },
          complete: function() {
            // complete
              console.log("失败2")
          }
        })*/
    },

    /**
     *  页面初始化
     * @param options  options为页面跳转所带来的参数
     */
    onLoad: function (options) {

        var that=this
        this.setData({
            userid:wx.getStorageSync('userid'),
            ename:wx.getStorageSync('ename'),
            phone:wx.getStorageSync('phone')
        })
        wx.request({
          url: 'http://app.benqmedicalcenter.com:8090/QueryCode.ashx?state=hospital',
          data: {},
          method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
          // header: {}, // 设置请求的 header
          success: function(res){
            // success
            that.setData({
               hospitalarrays:res.data
             })
              var arr=[]
              for(var i=0;i<res.data.length;i++)
              {
                  arr[i]=res.data[i].hospital
              }
              that.setData({
                  hospitalpk:arr
              })
                console.log(that.data.hospitalpk)
              console.log(that.data.array)
          },
          fail: function() {
            // fail
          },
          complete: function() {
            // complete
          }
        })
    },



})